package ua.at.tsvetkov.webservices.parsers;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import ua.at.tsvetkov.file.IO;
import ua.at.tsvetkov.util.Log;

/**
 * The base class for retrieve a data from web services for parsers.
 * 
 * @author Alexandr Tsvetkov
 */
public class WebParsersBase {

	/**
	 * Send request without waiting a response. Result will be printed at log.
	 * 
	 * @param urlStrl
	 */
	public static void sendInThread(final String urlStrl) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					URL url = new URL(urlStrl);
					HttpURLConnection connection = (HttpURLConnection) url.openConnection();
					InputStream in = connection.getInputStream();
					in.close();
					String result = connection.getResponseMessage();
					Log.v("Result " + result + " > " + urlStrl);
				} catch (Exception e) {
					Log.w(e);
				}
			}
		}).start();

	}

	/**
	 * Posting Content. To upload data to a web server.
	 * 
	 * @param urlStrl
	 * @param in input stream for posting content.
	 */
	public static void postData(String urlStrl, InputStream in) {
		URL url = null;
		HttpURLConnection urlConnection = null;
		BufferedInputStream inp = null;
		BufferedOutputStream out = null;
		try {
			url = new URL(urlStrl);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setChunkedStreamingMode(0);
			inp = new BufferedInputStream(in);
			out = new BufferedOutputStream(urlConnection.getOutputStream());
			while (inp.available() > 0) {
				byte[] buffer = new byte[IO.BUFFER];
				int bytesRead = -1;
				while ((bytesRead = inp.read(buffer)) != -1) {
					out.write(buffer, 0, bytesRead);
				}
			}
		} catch (Exception e) {
			Log.w(e);
		} finally {
			urlConnection.disconnect();
			try {
				inp.close();
				out.flush();
				out.close();
			} catch (IOException e) {
				Log.w(e);
			}
		}
	}

	/**
	 * Get InputStream from Internet by url.
	 * 
	 * @param urlStr
	 * @return InputStream
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	protected static InputStream getWebInputStream(String urlStr) throws MalformedURLException, IOException {
		URL url = new URL(urlStr);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		InputStream in = connection.getInputStream();
		return in;
	}

	/**
	 * Get InputStream from Internet by file name.
	 * 
	 * @param pathAndFileName
	 * @return InputStream
	 * @throws FileNotFoundException
	 */
	protected static InputStream getFileInputStream(String pathAndFileName) throws FileNotFoundException {
		File file = new File(pathAndFileName);
		InputStream in = new FileInputStream(file);
		return in;
	}

}
